package service

import (
    "context"
    "gitee.com/WisdomClassroom/CollegeStruct/glb"
    "gitee.com/WisdomClassroom/core"
    "gitee.com/WisdomClassroom/core/models"
    "gitee.com/WisdomClassroom/core/protobufs/v1/pb"
    "github.com/jinzhu/gorm"
    "time"
)

func (s *Service) CreateCollege(_ context.Context, in *pb.CreateCollegeRequest) (*pb.CreateCollegeResponse, error) {
    token := core.NewToken()
    err := token.UnpackToken(in.AuthToken, &core.FlagTokenCert)
    if err != nil {
        return &pb.CreateCollegeResponse{Status: &pb.ResponseStatus{
            Code: pb.ResponseStatusCode_NotAuth, Error: err.Error(),
        },}, nil
    }

    if token.Type != core.UserRoleTypeCodeRoot {
        return &pb.CreateCollegeResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "用户没有权限",
        },}, nil
    }
    userOfReq := models.UserModel{
        Model: models.Model{
            UUID: token.UUID,
        },
        Type: core.UserRoleTypeCodeRoot,
    }
    err = glb.DB.Scopes(notDeleted).Where(&userOfReq).First(&userOfReq).Error
    if err == gorm.ErrRecordNotFound {
        return &pb.CreateCollegeResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "用户没有权限或编号不存在",
        },}, nil
    } else if err != nil {
        glb.Logger.Error(err.Error())
        return createCollegeRespSvrErr, nil
    }

    if len(in.Name) == 0 {
        return &pb.CreateCollegeResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "学院名称未填写",
        },}, nil
    }
    if len(in.ManagerUUID) != 36 {
        return &pb.CreateCollegeResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "管理员编号不正确",
        },}, nil
    }

    userOfManagerOfArg := models.UserModel{
        Model: models.Model{
            UUID: in.ManagerUUID,
        },
    }
    err = glb.DB.Scopes(notDeleted).Where(&userOfManagerOfArg).First(&userOfManagerOfArg).Error
    if err == gorm.ErrRecordNotFound {
        return &pb.CreateCollegeResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "管理员编号不存在",
        },}, nil
    } else if err != nil {
        glb.Logger.Error(err.Error())
        return createCollegeRespSvrErr, nil
    }

    college := models.CollegeModel{
        Model: models.Model{
            UUID:         core.GenUUID(),
            CreationTime: time.Now().Unix(),
        },
        Name:        in.Name,
        Description: in.Description,
    }
    err = glb.DB.Transaction(func(tx *gorm.DB) error {
        if err := tx.Create(&college).Error; err != nil {
            return err
        }
        err = tx.Create(&models.CollegeManagerBindModel{
            Model: models.Model{
                UUID:         core.GenUUID(),
                CreationTime: time.Now().Unix(),
            },
            CollegeUUID: college.UUID,
            ManagerUUID: in.ManagerUUID,
        }).Error
        if err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        glb.Logger.Error(err.Error())
        return createCollegeRespSvrErr, nil
    }
    return &pb.CreateCollegeResponse{
        Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_Suc},
        UUID:   college.UUID,
    }, nil
}
